package com.xiaodu.acer.phoneplay.ui.adapter;

import android.view.View;
import android.widget.ImageView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.xiaodu.acer.phoneplay.R;
import com.xiaodu.acer.phoneplay.bean.downloadbean.DownloadMission;
import com.xiaodu.acer.phoneplay.common.Constant;
import com.xiaodu.acer.phoneplay.widget.DownloadButtonConntroller;
import com.xiaodu.acer.phoneplay.widget.DownloadProgressButton;

import library.imageloader.ImageLoader;
import retrofit2.Retrofit;


public class DownloadingAdapter extends BaseQuickAdapter<DownloadMission, BaseViewHolder> {

    private DownloadButtonConntroller mDownloadButtonConntroller;

    public DownloadingAdapter(Retrofit retrofit) {
        super(R.layout.template_app_downloading);
        mDownloadButtonConntroller = new DownloadButtonConntroller(retrofit);

        //加载时打开动画
        openLoadAnimation();
    }

    @Override
    protected void convert(BaseViewHolder helper, DownloadMission item) {

        ImageLoader.load(Constant.BASE_IMG_URL + item.getIcon(), (ImageView) helper.getView(R.id.img_app_icon));
        helper.setText(R.id.txt_app_name, item.getDisplayName());


        //绑定点击事件
        helper.addOnClickListener(R.id.btn_download);
        View viewBtn = helper.getView(R.id.btn_download);

        //防止button复用时状态跟着变
        if (item.isFlag()) {
            viewBtn.setVisibility(View.VISIBLE);
        } else {
            item.setFlag(true);
        }


        if (viewBtn instanceof DownloadProgressButton) {
            DownloadProgressButton btn = (DownloadProgressButton) viewBtn;
            mDownloadButtonConntroller.handClick(btn, item);
        }

    }
}